package com.example.lotterysystem.service.activitystatus.operator;

import com.example.lotterysystem.dao.dataobject.ActivityPrizeDO;
import com.example.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.example.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Spider-Man
 * Date: 2025-09-15
 * Time: 11:13
 */
@Component
public class PrizeOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 1;
    }


    /**
     * 校验奖品是否存在或者奖品状态是否需要进行转换
     */
    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //判断当前奖品状态是否不是 COMPLETED(未抽取)
        Long prizeId = convertActivityStatusDTO.getPrizeId();//奖品id
        Long activityId = convertActivityStatusDTO.getActivityId();//活动id
        //两个id定位到唯一的活动奖品关联表中的唯一奖品数据
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        if(prizeId == null ||
                null == activityId ||
                null == targetPrizeStatus) {
            return false;
        }
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectByAPId(activityId,prizeId);
        if(activityPrizeDO == null) {
            return false;//奖品信息为空
        }
        if(targetPrizeStatus.name().equalsIgnoreCase(activityPrizeDO.getStatus())) {
            return false; //奖品状态与目标状态一致
        }
        return true;

    }


    /**
     * 更新活动奖品关联表中的数据
     * @param convertActivityStatusDTO
     * @return
     */
    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        try {
            activityPrizeMapper.updateStatus(activityId,prizeId,targetPrizeStatus.name());
            return true;
        }catch (Exception e) {
            return false;
        }
    }
}
